package revise;


 class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
public class Practice {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null){
            return null;
        }
        //先删除除了同节点以外的节点
        ListNode prve = head;
        ListNode cur = head.next;
        while (cur != null){
            //判断是否要删除
            if (cur.val == val){
                prve.next = cur.next;
                cur = cur.next;
            } else {
                prve = prve.next;
                cur = cur.next;
            }
        }
        //最后删除头节点
        if (head.val == val){
            head = head.next;
        }
        return head;
    }
}
